package com.sebiotec.book.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;

import com.allen.library.base.BaseHttpUtil;
import com.allen.library.interceptor.Transformer;
import com.allen.library.observer.CommonObserver;
import com.sebiotec.basecommon.utils.constant.SjmConstant;
import com.sebiotec.basecommon.utils.language.LocalManageUtil;
import com.sebiotec.basecommon.utils.tools.ActivityTool;
import com.sebiotec.basecommon.utils.tools.SPUtils;
import com.sebiotec.book.model.response.UseProfileResponse;
import com.sebiotec.book.net.BookApiHelper;
import com.sebiotec.book.ui.view.LoadingDialog;

import java.util.HashMap;

import okhttp3.ResponseBody;

public class BasicViewModel extends AndroidViewModel {
    public BasicViewModel(@NonNull Application application) {
        super(application);
    }


    public MyBasicDataCallBack mDataCallBack;

    //数据回调接口
    public interface MyBasicDataCallBack<T> {
        void onDataSuccessCallBack( T t);
        void onDataFailCallBack(T t);
    }




  public  void getUser(MyBasicDataCallBack<Object> mDataCallBack){
      LoadingDialog loadingDialog = new LoadingDialog(ActivityTool.getInstance().getLastActivity());
      HashMap<String,Object> map=new HashMap<String, Object>();
      map.put("LoginId", SPUtils.get(SjmConstant.LOGINID,""));
      map.put("Token", SPUtils.get(SjmConstant.TOKEN,""));
      map.put("Lang", LocalManageUtil.getSMJSetLanguageLocales(ActivityTool.getInstance().getLastActivity()));
      BookApiHelper.geRadomApi()
              .getUser(map,SPUtils.get(SjmConstant.LOGINID,""))
              .compose(Transformer.<UseProfileResponse>switchSchedulers(loadingDialog))
              .subscribe(new CommonObserver<UseProfileResponse>() {
                  @Override
                  protected String setTag() {
                      return "BookBasic";
                  }

                  @Override
                  protected void onError(String errorMsg) {
                      mDataCallBack.onDataFailCallBack(errorMsg);
                  }



                  @Override
                  protected void onSuccess(UseProfileResponse s) {

                      mDataCallBack.onDataSuccessCallBack(s);
                  }
              });

  }


  public  void  save(UseProfileResponse bean,MyBasicDataCallBack<Object> mDataCallBack){
      LoadingDialog loadingDialog = new LoadingDialog(ActivityTool.getInstance().getLastActivity());
      HashMap<String,Object> map=new HashMap<String, Object>();
      map.put("LoginId", SPUtils.get(SjmConstant.LOGINID,""));
      map.put("Token", SPUtils.get(SjmConstant.TOKEN,""));
      map.put("Lang", LocalManageUtil.getSMJSetLanguageLocales(ActivityTool.getInstance().getLastActivity()));
      BookApiHelper.geRadomApi()
              .EditTextUser(map,SPUtils.get(SjmConstant.LOGINID,""),new BaseHttpUtil<UseProfileResponse>().convertVo2Json(bean))
              .compose(Transformer.<ResponseBody>switchSchedulers(loadingDialog))
              .subscribe(new CommonObserver<ResponseBody>() {
                  @Override
                  protected void onError(String errorMsg) {
                      mDataCallBack.onDataFailCallBack(errorMsg);
                  }

                  @Override
                  protected void onSuccess(ResponseBody s) {

                      mDataCallBack.onDataSuccessCallBack(s);
                  }
              });
  }




}
